
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>如何实现二叉树的Z字型（锯齿形）遍历回答的 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>如何实现二叉树的Z字型（锯齿形）遍历回答的 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">什么是二叉树的Z字型（或锯齿形）遍历？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">它是一种按层级遍历二叉树节点的方式，但其特殊之处在于遍历方向会逐层交替改变。具体来说，第一层从左到右遍历，第二层从右到左，第三层再从左到右，依此类推。</div>
          </div>
          <div class="card-source">来源: 文档开篇介绍</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">为什么在实现Z字型遍历时，双端队列（Deque）是理想的数据结构？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为Z字型遍历需要在不同层级之间灵活地从前端或后端访问节点，而双端队列（Deque）允许在队列的两端进行插入和删除操作，完美地满足了这一需求。</div>
          </div>
          <div class="card-source">来源: Z字型遍历的基本思路</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在Z字型遍历算法中，如何控制每一层的遍历方向？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">通过使用一个布尔值（例如 `leftToRight`）来标记和控制当前层的遍历方向。在每一层遍历完成后，将这个布尔值取反，从而为下一层切换方向。</div>
          </div>
          <div class="card-source">来源: Z字型遍历算法步骤</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">算法</div>
          <div class="card-question">当Z字型遍历的方向为“从左到右”时，节点及其子节点应如何操作？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">算法</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">从队列的左端取出当前节点进行处理，然后将其子节点按照“左子节点先入队，右子节点后入队”的顺序加入到队列的右端。</div>
          </div>
          <div class="card-source">来源: Z字型遍历算法步骤</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">算法</div>
          <div class="card-question">当Z字型遍历的方向为“从右到左”时，节点及其子节点应如何操作？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">算法</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">从队列的右端取出当前节点进行处理，然后将其子节点按照“右子节点先入队，左子节点后入队”的顺序加入到队列的左端。</div>
          </div>
          <div class="card-source">来源: Z字型遍历算法步骤</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">算法</div>
          <div class="card-question">在开始Z字型遍历的主循环之前，需要执行哪些初始化步骤？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">算法</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">需要进行两项初始化：1. 创建一个双端队列 `Deque`，并将二叉树的根节点入队。2. 创建一个布尔变量 `leftToRight` 并将其值设为 `True`，以表示第一层从左向右遍历。</div>
          </div>
          <div class="card-source">来源: Z字型遍历算法步骤</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
